/**
 * @description
 * @author zs
 * @date 2018/8/20
 *
 */
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import {atosStatisticsAction, atosStatisticsState} from './index';
import moment from 'moment';
import { CommonTable } from 'components';
import SearchList from './SeachList/SearchList';


const convertParams = (time) => {
    const isArr = Array.isArray(time) && time.length > 0;
    return {
        startTime: isArr ? time[0].format('YYYY-MM-DD 00:00:00') : '',
        endTime: isArr ? time[1].format('YYYY-MM-DD 23:59:59') : ''
    };
}
class AtoSStatistics extends Component{
    constructor(props) {
        super(props);
        this.state = {
            initTime: [moment(), moment()]
        };
        this.columns = [
            { title: '日期', dataIndex: 'day', key: 'day' },
            { title: '注册量', dataIndex: 'regNum', key: 'regNum' },
            { title: '总放款量', dataIndex: 'loanSum', key: 'loanSum' },
            { title: '新客申请量', dataIndex: 'newSubNum', key: 'newSubNum' },
            { title: '新客放款量', dataIndex: 'newLoanSum', key: 'newLoanSum' },
            { title: '老客申请量', dataIndex: 'oldSubNum', key: 'oldSubNum' },
            { title: '老客放款量', dataIndex: 'oldLoanSum', key: 'oldLoanSum' },
            { title: '新客逾期量', dataIndex: 'newUserOverdeu', key: 'newUserOverdeu' },
            { title: '老客逾期量', dataIndex: 'oldUserOverdeu', key: 'oldUserOverdeu' },
            { title: '新客放款转化(Atos)', dataIndex: 'a2s', key: 'a2s' },
        ];
    }

    handleSearch = (obj) => {
        const { time } = obj;
        const { getTableData } = this.props;
        const params = convertParams(time);
        getTableData({ ...params });
    }

    componentDidMount() {
        const { getTableData } = this.props;
        const params = convertParams(this.state.initTime)
        getTableData({ ...params });
    }
    componentWillUnmount() {
        const { setTableData } = this.props;
        setTableData([]);
    }

    render() {
        const { initTime } = this.state;
        const { tableData, loading } = this.props;
        return (
            <div>
                <SearchList handleSearch={this.handleSearch} initTime={initTime}/>
                <CommonTable columns={this.columns} dataSource={tableData} loading={loading} pagination={null}/>
            </div>
        );

    }
}

const mapStateToProps = (state) => {
    const { businessStatisticsState: { atosStatisticsState } } = state;
    return {
        tableData: atosStatisticsState['tableData'],
        loading: atosStatisticsState['loading']
    };
}
const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({
        getTableData: atosStatisticsAction.ASSDEGetTableData,
        setTableData: atosStatisticsAction.ASSDESetTableData
    }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(AtoSStatistics);